﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http.Dependencies;
using Se.Framework.Common;

namespace Se.Framework.Web
{
    public class ObjectRegistryScopeContainer : IDependencyScope
    {
        private readonly IDependencyResolver innerResolver;

        public ObjectRegistryScopeContainer(IDependencyResolver innerResolver)
        {
            this.innerResolver = innerResolver;
        }

        public object GetService(Type serviceType)
        {
            if (ObjectRegistry.Current.IsRegistered(serviceType))
                return ObjectRegistry.Current.Resolve(serviceType);

            return innerResolver.GetService(serviceType);

        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            if (ObjectRegistry.Current.IsRegistered(serviceType))
                return ObjectRegistry.Current.ResolveAll(serviceType);
            
            return innerResolver.GetServices(serviceType);
        }

        public void Dispose()
        {
            //when using 'return this' in BeginScope, this must be empty
        }
    }
}
